<?php

session_start();

include_once("connexion.php");

$data1="";
$data2="";
$data3="";
$data4="";
$data5="";
$data6="";
$data7="";
if (isset($_POST['rechercher']))
{

	$tarif_semaine = $_POST['tarif_semaine'];
	$tarif_journee = $_POST['tarif_journee'];
	$surface = $_POST['surface'];
	$id_type = $_POST['type'];
	$id_pays = $_POST['pays'];
	$id_ville = $_POST['ville'];
	$debut_location = $_POST["annee_debut"]."-".$_POST["mois_debut"]."-".$_POST["jour_debut"];
	$fin_location = $_POST["annee_fin"]."-".$_POST["mois_fin"]."-".$_POST["jour_fin"];

	if (empty($_POST['tarif_semaine']) && empty($_POST['tarif_journee']) && empty($_POST['surface']) && empty($_POST['type'])) {

		$sql_bien_recherche = "SELECT * FROM bien";
		$req_bien_recherche = mysqli_query($db, $sql_bien_recherche) or die ('Erreur SQL !'.$sql_bien_recherche.'<br>'.mysqli_error($db));
	}
	else
	{
		$sql_tarif = "SELECT * FROM tarif where tarif_jour=".$tarif_journee." AND tarif_semaine = ".$tarif_semaine."";
		$res_tarif = mysqli_query($db,$sql_tarif) or die('Erreur SQL !'.$sql_tarif.'<br>'.mysqli_error($db));
		$id_tarif = mysqli_fetch_array($res_tarif);
		$tarif = $id_tarif['id'];
		echo $tarif;

		$sql_bien_recherche1 = "SELECT * FROM bien where id_tarif = ".$tarif." and id_type = ".$id_type." and id_pays = ".$id_pays." and id_ville = ".$id_ville."";
		$req_bien_recherche1 = mysqli_query($db, $sql_bien_recherche1) or die ('Erreur SQL !'.$sql_bien_recherche1.'<br>'.mysqli_error($db));
	}
	
	
}

?>

<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="style.css">
		<meta charset="utf-8">
	</head>
	<body>
		<div id="conteneur">
			<div id="header"> <?php include_once("header.php"); ?><br/></div>
				<div id="contenu">
					<table border="1px" align="center">
						<form method="post">
							<tr>
								<td><p>Type de bien</p></td>
								<td><p>Tarifications semaine</p></td>
								<td><p>Tarifications journée</p></td>
								<td><p>Pays du bien</p></td>
								<td><p>Ville du bien</p></td>
								<td><p>Date de début de location</p></td>
								<td><p>Date de Fin de location</p></td>
								<td><p>Surface habitable souhaitée</p></td>
								
							</tr>
							<tr>
 								<td>
									<select name = "type">
										<option value="0">Choisissez</option>
										<option value="1">Maison</option>
										<option value="2">Appartement</option>
										<option value="3">Bungalow</option>
										<option value="4">Comping-Car</option>
										<option value="5">Mobilome</option>
										<option value="6">Emplacement</option>
										<option value="7">Chalet</option>
									</select>
								</td>
								<td>
									<input type="text" id="tarif" name="tarif_semaine" value=""/>
								</td>								
								<td>
									<input type="text" id="tarif" name="tarif_journee" value=""/>
								</td>
								<td>
									<select name = "pays">
										<option value="0">Choisissez</option>
										<option value="1">France</option>
										<option value="2">Espagne</option>
										<option value="3">Portugal</option>
										<option value="4">Allemagne</option>
										<option value="5">Angleterre</option>
									</select>
								</td>
								<td>
									<select name = "ville">
										<option value="0">Choisissez</option>
										<option value="1">Paris</option>
										<option value="2">Marseille</option>
										<option value="3">Madrid</option>
										<option value="4">Barcelone</option>
										<option value="5">Lisbone</option>
										<option value="6">Braga</option>
										<option value="7">Berlin</option>
										<option value="8">Essen</option>
										<option value="9">Londre</option>
										<option value="9">Liverpool</option>
									</select>
								</td>
								<td>
									<select name="jour_debut">
										    <?php for ($jour = 1 ; $jour <= 31 ; $jour++){ ?>
										    <option value="<?php echo $jour ?>"><?php echo $jour; ?></option>
											<?php } ?>  
										</select>
										<select name="mois_debut">
					        				<?php for ($mois = 1 ; $mois <= 12 ; $mois++){ ?>
										    <option value="<?php echo $mois ?>"><?php echo $mois; ?></option>
											<?php } ?>  
										</select>
										<select name="annee_debut">
										    <?php for ($annee = 2014 ; $annee <= 2017 ; $annee++){ ?>
										    <option value="<?php echo $annee ?>"><?php echo $annee; ?></option>
											<?php } ?>  
										</select>
								</td>
								<td>
									<select name="jour_fin">
										    <?php for ($jour = 1 ; $jour <= 31 ; $jour++){ ?>
										    <option value="<?php echo $jour ?>"><?php echo $jour; ?></option>
											<?php } ?>  
										</select>
										<select name="mois_fin">
					        				<?php for ($mois = 1 ; $mois <= 12 ; $mois++){ ?>
										    <option value="<?php echo $mois ?>"><?php echo $mois; ?></option>
											<?php } ?>  
										</select>
										<select name="annee_fin">
										    <?php for ($annee = 2014 ; $annee <= 2017 ; $annee++){ ?>
										    <option value="<?php echo $annee ?>"><?php echo $annee; ?></option>
											<?php } ?>  
										</select>
								</td>
								<td>
									<input type="text" id="surface" name="surface" value=""/>
								</td>
								<td colspan="2" align="center"><input type="submit" id="rechercher" name="rechercher"  value="Rechercher"></td>
							</tr>
							
								<?php 
									if (isset($_POST['rechercher'])) 
									{
										if (empty($_POST['tarif_semaine']) && empty($_POST['tarif_journee']) && empty($_POST['surface']) && empty($_POST['type'])) {

											while ($data = mysqli_fetch_array($req_bien_recherche))
				                          	{ 
				                          		
													$sql_type = "SELECT type FROM type where id=".$data['id_type'];
													$res_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db)); 
													$data1 = mysqli_fetch_array($res_type);

													$sql_tarif = "SELECT * FROM tarif where id=".$data['id_tarif'];
													$res_tarif = mysqli_query($db,$sql_tarif) or die('Erreur SQL !'.$sql_tarif.'<br>'.mysqli_error($db));
													$data2 = mysqli_fetch_array($res_tarif);

													$sql_pays = "SELECT pays FROM pays where id=".$data['id_pays'];
													$res_pays = mysqli_query($db,$sql_pays) or die('Erreur SQL !'.$sql_pays.'<br>'.mysqli_error($db)); 
													$data3 = mysqli_fetch_array($res_pays);

													$sql_user = "SELECT nom FROM user where id=".$data['id_user'];
													$res_user = mysqli_query($db,$sql_user) or die('Erreur SQL !'.$sql_user.'<br>'.mysqli_error($db));
													$data4 = mysqli_fetch_array($res_user);
													
													$sql_date = "SELECT * FROM reservation where id_bien=".$data['id'];
													$res_date = mysqli_query($db,$sql_date) or die('Erreur SQL !'.$sql_date.'<br>'.mysqli_error($db));
													$data6 = mysqli_fetch_array($res_date);


													$sql_ville = "SELECT ville FROM ville where id=".$data['id_ville'];
													$res_ville = mysqli_query($db,$sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db)); 
													$data7 = mysqli_fetch_array($res_ville);

					                                echo "  <form method='post'>
					                                            
					                                                <tr>
					                                                    <td>
					                                                        ".$data1['type']."
					                                                    </td>
					                                                    <td>
					                                                        ".$data2['tarif_semaine']." €
					                                                    </td>
					                                                    <td>
					                                                        ".$data2['tarif_jour']." €
					                                                    </td>
					                                                    <td>
					                                                        ".$data3['pays']."
					                                                    </td>
					                                                    <td>
					                                                      	".$data7['ville']."
					                                                    </td>
					                                                    <td>
					                                                        ".$data6['date_debut']."
					                                                    </td>
					                                                    <td>
					                                                        ".$data6['date_fin']."
					                                                    </td>
					                                                    <td>
					                                                    	".$data['surface']." m²
					                                                    </td>
					                                                </tr>
					                                        </form>";

				                          	}
										}
										else
										{
											

											while ($data = mysqli_fetch_array($req_bien_recherche1))
				                          	{ 
				                          		if ($fin['0'] != 1)
                            					{

					                                if ($id_pays != 0)
													{
														$sql_pays = "SELECT pays FROM pays where id=".$id_pays;
														$req_pays = mysqli_query($db, $sql_pays); 
						                                $data3 = mysqli_fetch_array($req_pays);
													}
													
													if ($id_ville != 0)
													{
														$sql_ville = "SELECT ville FROM ville where id=".$id_ville;
														$res_ville = mysqli_query($db, $sql_ville);
						                                $data4 = mysqli_fetch_array($res_ville);
													}
													
													if ($id_type != 0)
													{
														$sql_type = "SELECT type FROM type where id=".$id_type;
						                                $res_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db)); 
						                                $data1 = mysqli_fetch_array($res_type);
													}

					                                echo "  <form method='post'>
					                                            
					                                                <tr>
					                                                    <td>
					                                                        ".$data1['type']."
					                                                    </td>
					                                                    <td>
					                                                        ".$tarif_semaine." €
					                                                    </td>
					                                                    <td>
					                                                        ".$tarif_journee." €
					                                                    </td>
					                                                    <td>
					                                                        ".$data3['pays']."
					                                                    </td>
					                                                    <td>
					                                                      	".$data4['ville']."
					                                                    </td>
					                                                    <td>
					                                                        ".$debut_location."
					                                                    </td>
					                                                    <td>
					                                                        ".$fin_location."
					                                                    </td>
					                                                    <td>
					                                                    	".$data['surface']." m²
					                                                    </td>
					                                                </tr>
					                                        </form>";
				                                }
					                            else
					                            {
					                            	echo "<tr>
					                            				<p>Aucun bien ne correspond à votre recherche</p>
					                            		  </tr>
					                            		";
					                            }  
				                          	}
										}     	
			                        }	
			                	?>
							
						</form>
					</table>
					<table>
						
					</table>
				</div>
			<div id="footer"> <?php include_once("footer.html"); ?> </div>
		</div>
	</body>
</html>